package com.charity.controller;

import com.charity.domain.Project;
import com.charity.service.IProjectService;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private IProjectService projectService;

    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createProject(@RequestBody Project project) {
        Map<String, Object> response = new HashMap<>();
        if (projectService.createProject(project)) {
            response.put("code", 200);
            response.put("message", "创建成功");
        } else {
            response.put("code", 400);
            response.put("message", "创建失败");
        }
        return ResponseEntity.ok(response);
    }

    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateProject(@RequestBody Project project) {
        Map<String, Object> response = new HashMap<>();
        if (projectService.updateProject(project)) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 400);
            response.put("message", "更新失败");
        }
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/delete/{projectId}")
    public ResponseEntity<Map<String, Object>> deleteProject(@PathVariable Long projectId) {
        Map<String, Object> response = new HashMap<>();
        if (projectService.deleteProject(projectId)) {
            response.put("code", 200);
            response.put("message", "删除成功");
        } else {
            response.put("code", 400);
            response.put("message", "删除失败");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/detail/{projectId}")
    public ResponseEntity<Map<String, Object>> getProjectDetail(@PathVariable Long projectId) {
        Map<String, Object> response = new HashMap<>();
        Project project = projectService.getProjectById(projectId);
        if (project != null) {
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", project);
        } else {
            response.put("code", 404);
            response.put("message", "项目不存在");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getProjectList(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> response = new HashMap<>();
        List<Project> projects = projectService.getProjectList(status, pageNum, pageSize);
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", projects);
        return ResponseEntity.ok(response);
    }

    @PutMapping("/status/{projectId}")
    public ResponseEntity<Map<String, Object>> updateProjectStatus(
            @PathVariable Long projectId,
            @RequestParam Integer status) {
        Map<String, Object> response = new HashMap<>();
        if (projectService.updateProjectStatus(projectId, status)) {
            response.put("code", 200);
            response.put("message", "状态更新成功");
        } else {
            response.put("code", 400);
            response.put("message", "状态更新失败");
        }
        return ResponseEntity.ok(response);
    }
}